package com.xyh.service.impl;

import com.xyh.bean.Area;
import com.xyh.dao.IAreaDao;
import com.xyh.dao.impl.AreaDaoImpl;
import com.xyh.service.IAreaService;
import com.xyh.util.PageUtil;
import com.xyh.vo.AreaVO;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XiongYuhao
 * @Date: 2020/10/25 0025-下午 12:21
 * @Description: MyForestManagerSystem
 * @version：1.0
 */
public class AreaServiceImpl implements IAreaService {
    IAreaDao iad = new AreaDaoImpl();
    @Override
    public PageUtil<AreaVO> listByPage(int pageNo, int pageSize, String areaName, String treeType, String className) {
        int counts = iad.areaCounts(areaName,treeType,className);
        List<Area> areas = iad.listByPage(pageNo,pageSize,areaName,treeType,className);
        List<AreaVO> areaVO = new ArrayList<>();
        for(Area area:areas){
            AreaVO avo = new AreaVO();
            try {
                BeanUtils.copyProperties(avo,area);
                BeanUtils.copyProperty(avo,"className",area.getMc().getClassName());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            areaVO.add(avo);
        }
        PageUtil<AreaVO> pageBean = new PageUtil<>(areaVO,pageNo,pageSize,counts);
        return pageBean;
    }

    @Override
    public boolean add(Area area) {
        return iad.add(area)>0?true:false;
    }

    @Override
    public List<Area> queryAreaList() {
        return iad.queryAreaList();
    }
}
